import 'dart:math';

import 'package:flame/game.dart';
import 'package:flame/input.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'component/Background.dart';
import 'component/BulletTank.dart';
import 'component/Tank.dart';


class MyTank extends FlameGame with KeyboardEvents {
  late BackGround bg = BackGround();
  late TankModel tank;
  late List<BulletTank> bullets = [];
  late List<BulletTank> enemyBullets = [];
  late Size screenSize;
  late List<TankModel> gTanks = [];

  Future<void> onLoad() async {
    add(bg);

    initTank();
  }

  Future initTank() async {
    tank = TankFactory.createTank(this, TankID.MASTER);
    for (var i = 0; i < 5; i++) {
      var id = Random().nextInt(100) + 1;
      late TankID tankId;
      if (id <= 50) {
        tankId = TankID.ENEMY_ONE;
      } else {
        tankId = TankID.ENEMY_TWO;
      }

      gTanks.add(TankFactory.createTank(this, tankId));
    }
  }

  @override
  void render(Canvas canvas) {
    super.render(canvas);
    tank.render(canvas);
    //bg.render(canvas);
    if (bullets.isNotEmpty) {
      bullets.forEach((element) {
        element.render(canvas);
      });
    }
    if (enemyBullets.isNotEmpty) {
      enemyBullets.forEach((element) {
        element.render(canvas);
      });
    }
    if (gTanks.isNotEmpty) {
      gTanks.forEach((element) {
        element.render(canvas);
      });
    }
  }

  JoypadChange(Offset offset) {
    if (offset == Offset.zero) {
      tank.bodyAngleT = 0;
      //tank.paoAngleT = 0;
    } else {
      //得到车身的目标角度
      tank.bodyAngleT = offset.direction; //范围（pi,-pi）
      //tank.paoAngleT = offset.direction;
    }
  }

  @override
  KeyEventResult onKeyEvent(
    RawKeyEvent event,
    Set<LogicalKeyboardKey> keysPressed,
  ) {
    if (event is RawKeyDownEvent) {
      if (event.logicalKey == LogicalKeyboardKey.space) {
        onFireStart();
      }
      if (event.logicalKey == LogicalKeyboardKey.keyY) {
        onEnemyFire();
      }
    }
    return super.onKeyEvent(event, keysPressed);
  }

  onEnemyFire() {
    for(var i = 0;i < gTanks.length;i++){
      final element = gTanks[i];
final bullet = BulletTank(
        speed: 300,
        offset: element.position,
        myAngle: element.bodyAngle,
        id: element.tankId,
        screenSize: canvasSize);
    enemyBullets.add(bullet);
    }
    
  }

  onFireStart() {
    final bullet = BulletTank(
        speed: 400,
        offset: tank.position,
        myAngle: tank.bodyAngle,
        id: tank.tankId,
        screenSize: canvasSize);
    bullets.add(bullet);
  }
  
  double enemyTime = 0;

  @override
  void update(double dt) {
    super.update(dt);
    // enemyTime += dt;
    // if (enemyTime >= 3) {
    //   onEnemyFire();
    //   enemyTime = 0;
    // }
    tank.update(dt);
    if (bullets.isNotEmpty) {
      bullets.forEach((element) {
        element.update(dt);
      });
      bullets.removeWhere((element) => element.isHit || !element.isLive);
    }
    if (enemyBullets.isNotEmpty) {
      enemyBullets.forEach((element) {
        element.update(dt);
      });
      enemyBullets.removeWhere((element) => element.isHit || !element.isLive);
    }
    if (gTanks.isNotEmpty) {
      gTanks.forEach((element) {
        element.update(dt);
      });
      gTanks.removeWhere((element) => !element.isLive);
    }
  }

  @override
  void onGameResize(Vector2 vec) {
    super.onGameResize(vec);
    screenSize = vec.toSize();
    //bg.onGameResize(vec);
  }
}
